# coding=utf-8

import argparse

# 输入文件格式
# 每行一个条目，每组结束后以一个空行分隔
# 最后一组的后面，也要包含一个空行

# 输出文件格式
# 组号
# 条目,条目, ...

def proc1(input_file, output_file):
    with open(input_file, encoding="utf-8") as f, open(output_file, "w", encoding="utf-8") as of:
        lines = f.readlines()
        
#new_group = True
        group_id = 0
        items = []
        unique = set()
        for i, r in enumerate(lines, 1):
#            if i < 9:
#                continue
#            if i > 159:
#                break


            nr = r.strip()
            if nr == "":
                # print("%d\n" % group_id)
                # print("%s\n" % ",".join(items))
                of.write("%d\n" % group_id)
                of.write("%s\n" % ",".join(items))
                items.clear()
                unique.clear()
                group_id += 1
# last group end
# output group
#            new_group = True

            else:
                assert nr != ""
                if nr in unique:
                    print("duplicated: %s" % nr)
                else:
                    unique.add(nr)
                    items.append(nr)
            
            # print(i, nr)

def proc2(input_file, output_file):
    with open(input_file, encoding="utf-8") as f:
        lines = f.readlines()
        lib = []
        for lno in range(0, len(lines), 3):
            group_id = lines[lno].strip()
            hanzi = lines[lno + 1].strip().split(",")
            pinyin = [x.strip() for x in lines[lno + 2].split(",")]
            assert len(hanzi) == len(pinyin)
            for i, h in enumerate(hanzi):
                lib.append([h, pinyin[i], group_id])

    with open(output_file, "w", encoding="utf-8") as f:
        f.write("[\n")
        for h,p,g in lib:
        # for r in lib:
            f.write("    ('%s', '%s', %s),\n" % (h,p,g))
        f.write("]\n")

        
#    print("proc2 finish")
#    pass


if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Process some integers.')
    # parser.add_argument('integers', metavar='N', type=int, nargs='+',
    #                     help='an integer for the accumulator')
    # parser.add_argument('--sum', dest='accumulate', action='store_const',
    #                     const=sum, default=max,
    #                     help='sum the integers (default: find the max)')
    parser.add_argument('--func', '-f', dest='func', action='store', default=1, type=int,
                        help='select function')
    parser.add_argument('--input', '-i', dest='input', action='store', required=True, #default=1,
                        help='input file')
    parser.add_argument('--output', '-o', dest='output', action='store', required=True, #default=1,
                        help='output file')

    args = parser.parse_args()
    # print(args)
    assert args.func in (1, 2)
    print("input file:  %s" % args.input)
    print("output file: %s" % args.output)
    # exit(0)
    if args.func == 1:
        pass
        proc1(args.input, args.output)
        # print("input:  %s", args.input)
        # print("output: %s", args.output)
    elif args.func == 2:
        proc2(args.input, args.output)
    #proc1()
    # proc2()
